/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.authentication.domain.AuthLog;
import cn.ibizlab.core.authentication.service.AuthLogService;
import cn.ibizlab.core.authentication.filter.AuthLogSearchContext;

/**
 * 实体[AuthLog] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractAuthLogResource {

    @Autowired
    public AuthLogService authLogService;

    @Autowired
    @Lazy
    public AuthLogDTOMapping authLogDtoMapping;

    @Autowired
    @Lazy
    public AuthLogFilterDTOMapping authLogFilterDtoMapping;


    /**
    * 创建Create 登录日志
    * 
    *
    * @param dto dto
    * @return ResponseEntity<AuthLogDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"登录日志" },  notes = "AuthLog-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Create-all')")
    @PostMapping(value = {"auth_logs","auth_logs"})
    public ResponseEntity<AuthLogDTO> create
            (@Validated @RequestBody AuthLogDTO dto) {
        AuthLog domain = authLogDtoMapping.toDomain(dto);
        authLogService.create(domain);
        AuthLog rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(authLogDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 登录日志
    * 
    *
    * @param id id
    * @return ResponseEntity<AuthLogDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"登录日志" },  notes = "AuthLog-Get ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Get-all')")
    @GetMapping("auth_logs/{id}")
    public ResponseEntity<AuthLogDTO> getById
            (@PathVariable("id") Long id) {
        AuthLog rt = authLogService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(authLogDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 登录日志
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"登录日志" },  notes = "AuthLog-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Remove-all')")
    @DeleteMapping("auth_logs/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") Long id) {
        Boolean rt = authLogService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 登录日志
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<AuthLogDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"登录日志" },  notes = "AuthLog-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Update-all')")
    @PutMapping("auth_logs/{id}")
    public ResponseEntity<AuthLogDTO> updateById
            (@PathVariable("id") Long id, @Validated @RequestBody AuthLogDTO dto) {
        AuthLog domain = authLogDtoMapping.toDomain(dto);
        domain.setId(id);
        authLogService.update(domain);
        AuthLog rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(authLogDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 登录日志
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"登录日志" },  notes = "AuthLog-CheckKey ")
    @PostMapping("auth_logs/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody AuthLogDTO dto) {
        AuthLog domain = authLogDtoMapping.toDomain(dto);
        Integer rt = authLogService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 登录日志
    * 
    *
    * @param dto dto
    * @return ResponseEntity<AuthLogDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"登录日志" },  notes = "AuthLog-GetDraft ")
    @GetMapping("auth_logs/getdraft")
    public ResponseEntity<AuthLogDTO> getDraft
            (@SpringQueryMap AuthLogDTO dto) {
        AuthLog domain = authLogDtoMapping.toDomain(dto);
        AuthLog rt = authLogService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(authLogDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 登录日志
    * 
    *
    * @param dto dto
    * @return ResponseEntity<AuthLogDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"登录日志" },  notes = "AuthLog-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Save-all')")
    @PostMapping("auth_logs/save")
    public ResponseEntity<AuthLogDTO> save
            (@Validated @RequestBody AuthLogDTO dto) {
        AuthLog domain = authLogDtoMapping.toDomain(dto);
        authLogService.save(domain);
        AuthLog rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(authLogDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 登录日志
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<AuthLogDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"登录日志" },  notes = "AuthLog-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-FetchDefault-all')")
    @PostMapping("auth_logs/fetchdefault")
    public ResponseEntity<List<AuthLogDTO>> fetchDefault
            (@Validated @RequestBody AuthLogFilterDTO dto) {
        AuthLogSearchContext context = authLogFilterDtoMapping.toDomain(dto);
        Page<AuthLog> domains = authLogService.searchDefault(context) ;
        List<AuthLogDTO> list = authLogDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchLog 登录日志
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<AuthLogDTO>>
    */
    @ApiOperation(value = "查询FetchLog", tags = {"登录日志" },  notes = "AuthLog-FetchLog ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-FetchLog-all')")
    @PostMapping("auth_logs/fetchlog")
    public ResponseEntity<List<AuthLogDTO>> fetchLog
            (@Validated @RequestBody AuthLogFilterDTO dto) {
        AuthLogSearchContext context = authLogFilterDtoMapping.toDomain(dto);
        Page<AuthLog> domains = authLogService.searchLog(context) ;
        List<AuthLogDTO> list = authLogDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建认证日志
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Create-all')")
    @ApiOperation(value = "批量新建认证日志", tags = {"认证日志" },  notes = "批量新建认证日志")
	@PostMapping("auth_logs/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<AuthLogDTO> dtos) {
        authLogService.createBatch(authLogDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除认证日志
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Remove-all')")
    @ApiOperation(value = "批量删除认证日志", tags = {"认证日志" },  notes = "批量删除认证日志")
	@DeleteMapping("auth_logs/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<Long> ids) {
        authLogService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新认证日志
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Update-all')")
    @ApiOperation(value = "批量更新认证日志", tags = {"认证日志" },  notes = "批量更新认证日志")
	@PutMapping("auth_logs/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<AuthLogDTO> dtos) {
        authLogService.updateBatch(authLogDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存认证日志
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Save-all')")
    @ApiOperation(value = "批量保存认证日志", tags = {"认证日志" },  notes = "批量保存认证日志")
	@PostMapping("auth_logs/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<AuthLogDTO> dtos) {
        authLogService.saveBatch(authLogDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入认证日志
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-AuthLog-Save-all')")
    @ApiOperation(value = "批量导入认证日志", tags = {"认证日志" },  notes = "批量导入认证日志")
	@PostMapping("auth_logs/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<AuthLogDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(authLogService.importData(config,ignoreError,authLogDtoMapping.toDomain(dtos)));
    }

}
